'use strict';
const db = uniCloud.database()

exports.main = async (event, context) => {
  try {
    // 清空现有数据（可选）
    const shouldClearData = event.clearData || false
    if (shouldClearData) {
      await db.collection('add-content').where({}).remove()
      console.log('已清空现有数据')
    }
    
    // 插入测试数据
    const testPosts = [
      {
        _id: 'test-post-1',
        content_type: 'post',
        category: '讨论',
        content: '有没有人一起去图书馆学习的？求组队！',
        files: [],
        user_id: 'test-user-1',
        user_name: '学习小助手',
        publisher_name: '学习小助手',
        publisher: {
          nickname: '学习小助手',
          avatar: ''
        },
        user_info: {
          nickname: '学习小助手',
          username: 'test-user-1',
          avatar: ''
        },
        create_time: Date.now(),
        status: 'published',
        like_count: 12,
        comment_count: 5,
        pinned: false,
        violation_count: 0
      },
      {
        _id: 'test-post-2',
        content_type: 'post',
        category: '失物招领',
        content: '捡到一张校园卡，失主请联系。卡号后四位是1234',
        files: [],
        user_id: 'test-user-2',
        user_name: '热心同学',
        publisher_name: '热心同学',
        publisher: {
          nickname: '热心同学',
          avatar: ''
        },
        user_info: {
          nickname: '热心同学',
          username: 'test-user-2',
          avatar: ''
        },
        create_time: Date.now() - 3600000,
        status: 'reviewed_approved',
        like_count: 8,
        comment_count: 2,
        pinned: false,
        violation_count: 0
      },
      {
        _id: 'test-post-3',
        content_type: 'post',
        category: '问答',
        content: '请问有人知道计算机网络这门课的期末考试重点吗？',
        files: [],
        user_id: 'test-user-3',
        user_name: '考试焦虑者',
        publisher_name: '考试焦虑者',
        publisher: {
          nickname: '考试焦虑者',
          avatar: ''
        },
        user_info: {
          nickname: '考试焦虑者',
          username: 'test-user-3',
          avatar: ''
        },
        create_time: Date.now() - 7200000,
        status: 'pending_review',
        like_count: 15,
        comment_count: 8,
        pinned: true,
        violation_count: 0
      },
      {
        _id: 'test-post-4',
        content_type: 'post',
        category: '其他',
        content: '这是一个包含敏感词汇的帖子，需要审核处理',
        files: [],
        user_id: 'test-user-4',
        user_name: '违规用户',
        publisher_name: '违规用户',
        publisher: {
          nickname: '违规用户',
          avatar: ''
        },
        user_info: {
          nickname: '违规用户',
          username: 'test-user-4',
          avatar: ''
        },
        create_time: Date.now() - 10800000,
        status: 'hidden',
        like_count: 0,
        comment_count: 0,
        pinned: false,
        violation_count: 1,
        review_reason: '包含敏感内容'
      },
      {
        _id: 'test-post-5',
        content_type: 'post',
        category: '其他',
        content: '买卖学习资料，价格优惠，联系微信xxxxx',
        files: [],
        user_id: 'test-user-5',
        user_name: '商家用户',
        publisher_name: '商家用户',
        publisher: {
          nickname: '商家用户',
          avatar: ''
        },
        user_info: {
          nickname: '商家用户',
          username: 'test-user-5',
          avatar: ''
        },
        create_time: Date.now() - 14400000,
        status: 'reviewed_rejected',
        like_count: 2,
        comment_count: 1,
        pinned: false,
        violation_count: 2,
        review_reason: '包含商业广告内容',
        reject_reason: '违反社区规定，不允许发布商业广告'
      }
    ]
    
    // 批量插入数据
    for (const post of testPosts) {
      try {
        await db.collection('add-content').doc(post._id).set(post)
        console.log(`插入测试数据: ${post._id}`)
      } catch (error) {
        if (error.code === 'DUPLICATE_KEY') {
          // 如果数据已存在，更新它
          await db.collection('add-content').doc(post._id).update(post)
          console.log(`更新测试数据: ${post._id}`)
        } else {
          throw error
        }
      }
    }
    
    return {
      code: 200,
      msg: '测试数据初始化成功',
      data: {
        insertedCount: testPosts.length,
        posts: testPosts
      }
    }
    
  } catch (error) {
    console.error('初始化测试数据失败:', error)
    return {
      code: 500,
      msg: '初始化测试数据失败',
      error: error.message
    }
  }
}
